﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmChucVu : Form
    {
        ChucVuMgt chucvuMgt = new ChucVuMgt();
        private bool isEdit = false;

        public FrmChucVu()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            pnlTop.Enabled = true;
            txtTen.Text = "";
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtTen.Text))
            {
                ResultObj result = new ResultObj();

                string sTen = txtTen.Text;

                ChucVu chucvu = new ChucVu();
                chucvu.TenChucVu = sTen;

                if (!isEdit)
                {
                    result = chucvuMgt.SaveNewChucVu(chucvu);
                    chucvu.Id = result.ReturnCode;
                }
                else
                {
                    chucvu.Id = Convert.ToInt32(lblId.Text);
                    result = chucvuMgt.UpdateChucVu(chucvu);
                    isEdit = false;
                }

                if (result.ReturnCode > 0)
                {
                    MessageBox.Show(" Lưu thành công", "Thông báo");

                    btnSua.Enabled = false;
                    btnLuu.Enabled = false;

                    pnlTop.Enabled = false;
                    txtTen.Text = "";
                }
                else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                {
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                    txtTen.Select();
                }
                else
                {
                    MessageBox.Show("Thất bại");
                    txtTen.Select();
                }

                BindingToGrid();
            }
            else
            {
                MessageBox.Show("Vui lòng nhập tên chức vụ", "Thông báo");
                return;
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            isEdit = true;
            pnlTop.Enabled = true;
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            txtTen.Select();
        }

        private void BindingToGrid()
        {
            dgvChucVu.AutoGenerateColumns = false;
            dgvChucVu.DataSource = GetAllChucVu();
        }

        private void dgvChucVu_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            txtTen.Text = dgvChucVu.CurrentRow.Cells[0].Value.ToString();
            btnSua.Enabled = true;
        }

        public List<ChucVu> GetAllChucVu()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chucvuMgt.GetAllChucVu();
            List<ChucVu> listChucVu = (List<ChucVu>)resultObj.Result;

            return listChucVu;
        }

        private void FrmChucVu_Load(object sender, EventArgs e)
        {
            pnlTop.Enabled = false;
            btnLuu.Enabled = false;
            btnSua.Enabled = false;
            BindingToGrid();
        }
    }
}
